<template>
  <div class="user_right fl ml20">
    <el-form
      :model="modification"
      :rules="rules"
      ref="modification"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="旧密码" prop="old_password">
        <el-input type="password" class="ipt250" v-model="modification.old_password"></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="new_password">
        <el-input type="password" class="ipt250" v-model="modification.new_password"></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="confirm_password">
        <el-input type="password" class="ipt250" v-model="modification.confirm_password"></el-input>
      </el-form-item>
    </el-form>
    <div class="btn_pas">
      <div onselectstart="return false;" @click="submitForm('modification')">保存</div>
      <div onselectstart="return false;" @click="replacement">重置</div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.modification.confirm_password !== "") {
          this.$refs.modification.validateField("confirm_password");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.modification.new_password) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      modification: {
        old_password: "",
        new_password: "",
        confirm_password: ""
      },
      rules: {
        new_password: [
          { required: true, validator: validatePass, trigger: "blur" }
        ],
        confirm_password: [
          { required: true, validator: validatePass2, trigger: "blur" }
        ],
        old_password: [
          { required: true, message: "请输入旧密码", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    replacement() {
      for (let key in this.modification) {
        this.modification[key] = "";
      }
    },
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          alert("submit!");
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    }
  }
};
</script>


<style lang="scss">
.user_right {
  padding-bottom: 50px;
}
.ipt250 {
  width: 250px;
  margin-left: 40px;
  .el-input__inner {
    height: 32px;
    line-height: 32px;
  }
}
.demo-ruleForm {
  margin-top: 95px;
  .el-form-item {
    margin-bottom: 25px;
    .el-form-item__error {
      left: 190px;
    }
  }
  .el-form-item__label {
    font-size: 18px;
    &::after {
      content: ":";
      margin-left: 3px;
      font-weight: 600;
    }
  }
}
.el-form-item__label {
  width: 250px !important;
}
.btn_pas {
  display: flex;
  justify-content: center;
  margin-top: 146px;
  > div {
    width: 120px;
    height: 40px;
    text-align: center;
    line-height: 40px;
    border-radius: 5px;
    font-size: 18px;
    color: #fff;
    cursor: pointer;
    position: relative;
    transition: all 0.2s ease-in-out;
    &:nth-child(1) {
      background: #4448f8;
      margin-right: 25px;
      &:hover {
        background: #777acc9d;
      }
    }
    &:nth-child(2) {
      background: #e5e5e5;
      margin-left: 25px;
      &:hover {
        background: #5f5959;
      }
    }
  }
}
</style>